﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Resources;
using System.Globalization;
using System.Reflection;
using Microsoft.Win32;

namespace VsTortoiseSVN.Dialogs
{
    public partial class AboutDialog : Form
    {
        private const string UpdateCaption = "VsTortoise Update";

        /// <summary>
        /// Constructor
        /// </summary>
        public AboutDialog()
        {
            InitializeComponent();
            Text += Globals.AppTitle;

            Webupdate.Instance.Completed += new RunWorkerCompletedEventHandler(WebupdateCompleted);
        }

        void PrintInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();

            // VsTortoise Build Number
            stringBuilder.AppendLine(Globals.AppTitle + " (" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + ")");
            stringBuilder.AppendLine();

            // VsTortoise required DLLs
            CultureInfo dteCulture = new CultureInfo(Globals.DTE.LocaleID);
            CheckInstalledFile(string.Format("{0}\\VsTortoise.resources.dll", dteCulture.Name), stringBuilder);
            CheckInstalledFile(string.Format("{0}\\VsTortoise.resources.dll", dteCulture.TwoLetterISOLanguageName), stringBuilder);
            //CheckInstalledFile("SharpPlink-Win32.svnExe", stringBuilder);
            CheckInstalledFile("SharpSvn.dll", stringBuilder);
            //CheckInstalledFile("SharpSvn-DB44-20-Win32.dll", stringBuilder);
            //CheckInstalledFile("SharpSvn-SASL21-23-Win32.dll", stringBuilder);
            stringBuilder.AppendLine();

            // TortoiseSVN
            stringBuilder.AppendLine(string.Format("TortoiseProc.IsAvailable: {0}", TortoiseProc.Instance.IsAvailable));
            if (TortoiseProc.Instance.IsAvailable)
            {
                stringBuilder.AppendLine(string.Format("TortoiseProc.FilePath: {0}", TortoiseProc.Instance.FilePath));
                stringBuilder.AppendLine(string.Format("TortoiseProc.Is150OrNewer: {0}", TortoiseProc.Instance.Is150OrNewer));
                stringBuilder.AppendLine(string.Format("TortoiseProc.VersionInfo: {0}", TortoiseProc.Instance.VersionInfo.FileVersion));
                stringBuilder.AppendLine(string.Format("TortoiseProc.ExternalDiffViewer: {0}", string.IsNullOrEmpty(TortoiseProc.Instance.ExternalDiffViewer) ? "" : TortoiseProc.Instance.ExternalDiffViewer));
            }
            stringBuilder.AppendLine();

            // System Information
            GetSystemInfo(stringBuilder);
            stringBuilder.AppendLine();

            textVersion.Text = stringBuilder.ToString();
        }

        // Checks if the file exists that should have been installed
        // together with VsTortoise
        bool CheckInstalledFile(string filename, StringBuilder dest)
        {
            Assembly myAssembly = Assembly.GetExecutingAssembly();
            string folder = Path.GetDirectoryName(myAssembly.Location);

            if (File.Exists(Path.Combine(folder, filename)))
            {
                dest.AppendLine(filename + " found");
                return true;
            }
            else
            {
                dest.AppendLine(filename + " missing");
                return false;
            }
        }

        // Gets infos that are from importance for bug-reports
        void GetSystemInfo(StringBuilder dest)
        {
            dest.AppendLine(string.Format("Environment.OSVersion: {0}", Environment.OSVersion.VersionString));
            dest.AppendLine(string.Format("Environment.Version: {0}", Environment.Version));
            dest.AppendLine(string.Format("SystemInformation.MonitorCount: {0}", SystemInformation.MonitorCount));
            dest.AppendLine(string.Format("SystemInformation.Network: {0}", SystemInformation.Network));
            dest.AppendLine(string.Format("CultureInfo.CurrentCulture: {0}", CultureInfo.CurrentCulture));
            dest.AppendLine(string.Format("CultureInfo.CurrentUICulture: {0}", CultureInfo.CurrentUICulture));
            dest.AppendLine(string.Format("DTE.Version: {0}", Globals.DTE.Version));
            dest.AppendLine(string.Format("DTE.Name: {0}", Globals.DTE.Name));
            dest.AppendLine(string.Format("DTE.Edition: {0}", Globals.DTE.Edition));
            dest.AppendLine(string.Format("DTE.LocaleID: {0} ({1})", Globals.DTE.LocaleID, new CultureInfo(Globals.DTE.LocaleID).Name));
            dest.AppendLine(string.Format("DTE.FullName: {0}", Globals.DTE.FullName));
            dest.AppendLine(string.Format("DTE.Mode: {0}", Globals.DTE.Mode));
            dest.AppendLine(string.Format("DTE.DisplayMode: {0}", Globals.DTE.DisplayMode));
            dest.AppendLine(string.Format("DTE.RegistryRoot: {0}", Globals.DTE.RegistryRoot));
            dest.AppendLine(string.Format("DTE.CommandLineArguments: {0}", Globals.DTE.CommandLineArguments));

            foreach(AddIn addin in Globals.DTE.AddIns)
                dest.AppendLine(string.Format("DTE.AddIns: {0} (Connected: {1})", addin.Name, addin.Connected));
        }

        private void WebupdateCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            buttonUpdate.Enabled = true;
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            Webupdate.Instance.Start(true);
            buttonUpdate.Enabled = false;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Win32.ShellExecute("http://www.console-dev.de", "");
        }

        private void linkLabelHomepage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Win32.ShellExecute("http://vstortoise.codeplex.com", "");
        }

        private void AboutDialog_Shown(object sender, EventArgs e)
        {
            PrintInfo();
        }
    }
}
